import wilberforce.util.video.flvVideoController;
import wilberforce.event.simpleEventDispatcher;
import wilberforce.util.movieclip.MovieClipUtil;
import wilberforce.util.drawing.styles.fillStyle;
import wilberforce.util.drawing.styles.lineStyle;
import wilberforce.math.rect;
import wilberforce.util.drawing.drawingUtility;
import wilberforce.util.textField.textFieldUtility;

class wilberforce_demos.video.videoPlayer extends MovieClip
{
	var _videoController:flvVideoController;
	var _video;//:Video;
		
	static var playbackBarHeight:Number=10;
	static var playbackBarMargin:Number=10;
	static var videoControlsSpacing:Number=10;
	
	var playbackBarFillStyle:fillStyle;
	var playbackBarLineStyle:lineStyle;
	var playbackbarRect:rect;
	
	var playbackPercentageClip:MovieClip;
	var videoControlButtons:MovieClip;
	var timeDisplayTextField:TextField;
	
	var timeDisplayWidth:Number=200;
	
	function videoPlayer()
	{
		_videoController=new flvVideoController(_video);
		
		_videoController.addEventListener("videoSize",this,setVideoSize);
		//playbackController.addEventListener("videoPercentBufferFull",this,bufferFullUpdate);
		_videoController.addEventListener("videoPercentElapsed",this,videoPercentElapsed);
		_videoController.addEventListener("videoTimeElapsed",this,updateTimeDisplay);
		_videoController.addEventListener("videoPlaying",this,videoPlaying);
		
		
		
		_videoController.addEventListener("videoBuffering",this,videoBuffering);
		
		_videoController.addEventListener("videoPaused",this,videoPaused);
		_videoController.addEventListener("videoUnpaused",this,videoPlaying);
		
		playbackPercentageClip=MovieClipUtil.createEmptyClip(this);
		
		playbackBarLineStyle=new lineStyle(1,0xFFFFFF,30);
		playbackBarFillStyle=new fillStyle(0x000000,50,0);
		
		playbackbarRect=new rect(_video._x,_video._y,_video._width,_video._y+playbackBarHeight);
		
		timeDisplayTextField=textFieldUtility.createTextField(this,0,0,timeDisplayWidth,50,textFieldUtility.defaultRightAlignedTextFormat);
	}

	public function playFlv(videoUrl:String,srtSubtitlesUrl:String):Void
	{
		_videoController.playFlv(videoUrl);
		if (srtSubtitlesUrl)
		{
			
		}
	}
	
	public function updateTimeDisplay(time:Number,duration:Number)
	{

		timeDisplayTextField.text=formatTimeString(time)+" / "+formatTimeString(duration);
	}
	
	private function formatTimeString(time:Number):String
	{
		var seconds=Math.floor(time);
		var minutes=Math.floor(seconds/60);
		seconds-=minutes*60;
		var secondsString:String=""+seconds;
		if (secondsString.length<2) secondsString="0"+secondsString;
		var tString=""+minutes+":"+secondsString;
		return tString;
	}
	
	function positionVideoControls():Void
	{
		videoControlButtons._y=_video._height+videoControlsSpacing;
		timeDisplayTextField._y=videoControlButtons._y;
		timeDisplayTextField._x=_video._width-timeDisplayWidth;
	}
	
	function setVideoSize(tWidth,tHeight):Void
	{	
		_video._width=tWidth;
		_video._height=tHeight;
		positionVideoControls();
	}
	
	function bufferFullUpdate(tPercent:Number):Void
	{
		trace("Percent full "+tPercent);
	}
	
	function videoPlaying():Void
	{
		videoControlButtons.changeState("pause");
	}
	
	function videoPaused():Void
	{
		trace("Video paused");
		videoControlButtons.changeState("unpause");
	}
	
	function videoBuffering():Void
	{
		videoControlButtons.changeState("buffering");
	}
	
	function pause():Void
	{
		trace("vidplayer pause called");
		_videoController.pause();
	}
	
	function unpause():Void
	{
		trace("vidplayer pause called");
		_videoController.unpause();
	}
	
	function videoPercentElapsed(tPercent:Number):Void
	{
		/*
		playbackPercentageClip.clear();
		playbackbarRect.x=playbackBarMargin;
		playbackbarRect.y=_video._height-playbackBarMargin-playbackBarHeight;
		playbackbarRect.width=tPercent*(_video._width-playbackBarMargin*2);
		playbackbarRect.height=playbackBarHeight;
		
		playbackPercentageClip.clear();
		drawingUtility.drawRect(playbackPercentageClip,playbackbarRect,playbackBarLineStyle,playbackBarFillStyle);
		*/
		//trace("Percent elapsed "+tPercent);
	}

	
}